<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Models_Mysql_Page extends Modules_Core_Models_Page
{
	public function getById($id)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_page 
							WHERE page_id = ' . $this->_conn->escape($id) . ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
	
	public function dbPages($fieldsName = null)
	{
		$fieldsName = (null == $fieldsName) ? '*' : $fieldsName;
		$sql = "SELECT $fieldsName FROM " . $this->_conn->_tablePrefix . "core_page";
		$rs = $this->_conn->query($sql);
		$pages = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$pages[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $pages;
	}
	
	public function add($page)
	{
		$id = $this->_conn->insert($page, $this->_conn->_tablePrefix . 'core_page');
		return $id;	
	}
	
	public function find($condition = array(), $offset = null, $limit = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_page';
		
		if ($offset !== null && $limit !== null) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}
		$rs = $this->_conn->query($sql);
		$users = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$users[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $users;
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(*) as num_pages FROM ' . $this->_conn->_tablePrefix . 'core_page';
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_pages'];
	}
	
	public function update($page)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'core_page 
					SET name = "%s", description = "%s", route = "%s"
					WHERE page_id = "%d"';
		$sql = sprintf($sql, $this->_conn->escape($page['name']),
							$this->_conn->escape($page['description']), 
							$this->_conn->escape($page['route']),
							$this->_conn->escape($page['page_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function delete($pageId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'core_page 
							WHERE page_id = "' . $this->_conn->escape($pageId) . '"';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
}